package com.carryking.smart.rpc.netty.channel;

import com.carryking.smart.rpc.entity.RequestEntity;
import com.carryking.smart.rpc.utils.ServerInvokUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by jincarry on 2018/1/10.
 */
@Slf4j
public class ServerChannel extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof RequestEntity) {
            ctx.write(ServerInvokUtils.invok((RequestEntity) msg));
        }else{
            ctx.fireChannelRead(msg);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.channel().close();
        log.error("发生异常,链接关闭",cause);
    }
}
